<?php
namespace app\index\controller;


use think\Config;
use think\Db;
use think\Request;

class Center extends Base
{



    public function index()
    {
    $info = $this->user;
    $shop = model('shop')->where('owner',$this->uid)->find();
    $orderCount = 0;
    $revenue = 0;
    if($shop){
        $time1 = strtotime('today');
        $time2 = time();
        //今日订单
        $orders = model('order')
            ->where('shopId',$shop->id)
            ->where('payTime','>=',$time1)
            ->where('payTime','<=',$time2)
            ->where('status','>',0)
            ->where('status','<>',8)
            ->select();
        $orderCount = count($orders);
        //今日营收
        $revenue = 0;
        if($orders){
            foreach ($orders as $order){
                $revenue = $revenue + ($order->totalFee) - ($order->costPrice);
            }
        }
    }

    $this->assign('shop',$shop);
    $this->assign('revenue',$revenue);
    $this->assign('orderCount',$orderCount);
    $this->assign('info',$info);
    return view('index');
    }



    public function moneybag(){
        //钱包
        $recharges = model('recharge')->getSaleRecharge();
        $this->assign('recharges',$recharges);
        return view('moneybag');
    }



    public function buyWaitPay(){
        //待支付
        $data = model('order')->getUserOrder($this->uid,0);
        $this->assign('data',$data);
        return view('buyWaitPay');
    }

    public function cancelOrder($id){
        model('order')->where('buyUid',$this->uid)->where('id',$id)->where('status',0)->delete();
        $this->redirect('buyWaitPay');
    }

   public function buyWaitSend(){
        //待发货
       $data = model('order')->getUserOrder($this->uid,1);
       $this->assign('data',$data);
       return view('buyWaitSend');
    }



   public function buyWaitReceive(){
        //待收货
       $data = model('order')->getUserOrder($this->uid,2);
       $this->assign('data',$data);
       return view('buyWaitReceive');
    }


  public function buySuccess(){
        //完成
      $data = model('order')->getUserOrder($this->uid,3);
      $this->assign('data',$data);
      return view('buySuccess');

  }

  public function buyWaitComment(){
        //待评价
      $data = model('order')->getUserWaitComment($this->uid);
      $this->assign('data',$data);
      return view('buyWaitComment');
  }


   public function comment($id){
        //$id 是 orderId
       $this->assign('orderId',$id);
       return view('comment');
   }


    public function saveComment(){
        $data = Request::instance()->post();
        $order = model('order')->where('id',$data['orderId'])->find();
        $img = Request::instance()->file('img');
        $data['uid'] = $this->uid;
        $data['shopId'] = $order->shopId;
        $data['productId'] = $order->productId;
        $data['createTime'] = time();
        if($img){
            $data['img'] = upload($img);
        }
        model('comment')->insertGetId($data);
        $this->redirect('buyWaitComment');
    }


     public function saleWaitRefund(){

         $data = model('order')->homeGetShopOrder($this->uid,'jiujiu_order.status2>0 and jiujiu_order.status2<7');
         $this->assign('data',$data);

        return view('saleWaitRefund');
     }




     public function saleWaitSend(){
         $data = model('order')->homeGetShopOrder($this->uid,'jiujiu_order.status=1');
         $this->assign('data',$data);

        return view('saleWaitSend');
    }


    public function saleWaitReceive(){
        $data = model('order')->homeGetShopOrder($this->uid,'jiujiu_order.status=2');
        $this->assign('data',$data);
        return view('saleWaitReceive');
    }

    public function saleSuccess(){

        $data = model('order')->homeGetShopOrder($this->uid,'jiujiu_order.status=3');
        $this->assign('data',$data);
        return view('saleSuccess');

    }


    public function saleComment(){
        $commentType = Config::get('comment_type');
        $comments = model('order')->getShopOrderComment($this->uid);
        $this->assign('comments',$comments);
        $this->assign('commentType',$commentType);
        return view('saleComment');
    }








    public function reportSale(){
            //销售统计
        $shop = $this->getMyShop();
        //总订单
        $totalCount = model('order')->where('shopId',$shop->id)->where('status','>',0)->count();
        //待发货订单
        $waitCount = model('order')->where('shopId',$shop->id)->where('status',1)->count();
        //待收货订单
        $receiveCount = model('order')->where('shopId',$shop->id)->where('status',2)->count();
        //已完成订单
        $successCount = model('order')->where('shopId',$shop->id)->where('status',3)->count();
        //退款订单
        $refundCount = model('order')->where('shopId',$shop->id)->where('status',8)->count();

       $this->assign('totalCount',$totalCount);
       $this->assign('waitCount',$waitCount);
       $this->assign('receiveCount',$receiveCount);
       $this->assign('successCount',$successCount);
       $this->assign('refundCount',$refundCount);

        return view('reportSale');
    }






    public function queueOrder(){
            //派单记录
        $paidans = model('paidan')->getUserPaidan($this->uid);
        $this->assign('paidans',$paidans);
        return view('queueOrder');
    }




    public function goodsShelves(){
        //我的货架
        $shop = $this->getMyShop();
        $products = model('product')->getShopAllProduct($shop->id);
        $this->assign('products',$products['products']);
        $this->assign('guige',$products['guige']);
        $this->assign('shop',$shop);
        return view('goodsShelves');
    }



    public function myShop(){
        //我的店铺
        $shop = $this->getMyShop();
        $this->assign('shop',$shop);
        return view('myShop');

    }






    public function help(){
            //帮助中心
        return view('help');
    }






    public function statistics(){
            //销售统计
        return view('statistics');
    }





    public function agent(){

        $shop = $this->getMyShop();
        $agents = Db::table('jiujiu_shop_product')
            ->field(['jiujiu_user.*'])
            ->join('jiujiu_product','jiujiu_product.id=jiujiu_shop_product.productId','LEFT')
            ->join('jiujiu_user','jiujiu_user.uid=jiujiu_product.uid','LEFT')
            ->where('jiujiu_shop_product.shopId',$shop->id)
            ->where('jiujiu_product.uid','<>',$this->uid)
            ->select();
         $this->assign('agents',$agents);
         return view('agent');
    }



    public function agent2(){

        $shop = $this->getMyShop();
        $agents = Db::table('jiujiu_shop_product')
            ->field(['jiujiu_user.*'])
            ->join('jiujiu_product','jiujiu_product.id=jiujiu_shop_product.productId','LEFT')
            ->join('jiujiu_user','jiujiu_user.uid=jiujiu_product.uid','LEFT')
            ->where('jiujiu_shop_product.shopId','<>',$shop->id)
            ->where('jiujiu_product.uid',$this->uid)
            ->select();
        $this->assign('agents',$agents);
         return view('agent2');
    }



    public function mySet(){

        return view('mySet');
    }

    public function myAddress(){
        $addresses = model('address')->getUserAddress($this->uid);
        $this->assign('addresses',$addresses);
        return view('myAddress');
    }

    public function addAddress(){
        $country = model('city')->getAllCountry();
        $this->assign('country',$country);
        return view('addAddress');
    }


    public function saveAddress(){

        $data = Request::instance()->post();
        $data['uid'] = $this->uid;
        $data['createTime'] = time();
        model('address')->insert($data);
        $this->redirect('myAddress');

    }


    public function refundOrder($id){
        $order = model('order')->where('id',$id)->find();
        $this->assign('order',$order);
        return view('refundOrder');
    }



    public function saveRefund(){
        $data = Request::instance()->post();
        $img = Request::instance()->file('img');
        model('refund')->saveRefund($data,$img,$this->uid);
        $this->redirect('refundDetail',['id'=>$data['orderId']]);

    }


    public function refundDetail($id){
        if(!$id){
            $id = Request::instance()->get('id');
        }
        $refund = model('refund')->where('orderId',$id)->find();
        $this->assign('refund',$refund);
        return view('refundDetail');
    }



    public function successOrder($id){
        if(!$id){
            $id = Request::instance()->get('id');
        }
        user_success_order($id);
        $this->redirect('buySuccess');
    }




    public function changeRefund($id,$rs){

        changeRefund($id,$rs);
        $this->redirect('saleWaitRefund');
    }





    public function sendOrder($id){
        $this->assign('expressCompany',Config::get('express'));
        $this->assign('id',$id);
        return view('sendOrder');
    }



    public function saveSendOrder(){

        $data = Request::instance()->post();
        model('order')->sendOrder($data['id'],$data['expressCompany'],$data['expressNum']);
        $this->success('成功',url('index/center/saleWaitSend'));

    }










}
